//home模块的小仓库
import { reqGetGoodsInfo } from '@/api'
import { reqAddOrUpdateShopCart } from '@/api'
import { getUUID } from '@/utils/uuid_token'



//state:仓库存储数据的地方
const state = {
    goodsInfo: {},
    //游客的临时身份
    // uuid_token: getUUID()

}

//actions:处理action的地方，可以书写自己的业务逻辑吗，也可以处理异步
const actions = {
    //这里可以书写业务逻辑，但是不能直接修改state
    //获取产品信息
    async getGoodsInfo({ commit }, skuid) {
        let result = await reqGetGoodsInfo(skuid)
        if (result.code == 200) {
            commit('GETGOODSINFO', result.data)
        }

    },
    //将产品添加到购物车
    async addOrUpdateShopCart({ commit }, {skuId, skuNum}) {
        let result = await reqAddOrUpdateShopCart(skuId, skuNum)
        if(result.code == 200){
            return 'ok'
        }else{
            return Promise.reject(new Error('fail'))
        }

    },
    


}

//mutations:修改state的唯一手段
const mutations = {
    //通过API里面的接口函数调用，向服务器发送请求，获取服务器数据
    GETGOODSINFO(state, goodsInfo) {
        state.goodsInfo = goodsInfo
    }

}



//getters:理解为计算属性，用于简化仓库数据，让组件获取仓库的数据更加方便
const getters = {
    categoryView(state){
        return state.goodsInfo.categoryView || {}
    },
    skuInfo(state){
        return state.goodsInfo.skuInfo || {}
    },
    spuSaleAttrList(state){
        return state.goodsInfo.spuSaleAttrList || []
    },
    valuesSkuJson(state){
        return state.goodsInfo.valuesSkuJson || ""
    },
}

export default {
    state,
    actions,
    mutations,
    getters
}